const servicesModel = require('../model/servicesModel');
const shopsModel = require('../model/shopsModel')
//查询所有
module.exports.findAll = async function (searchObj, page) {
    let { current, size } = page;
    //获取数据总数
    let total = await servicesModel.countDocuments(searchObj);
    //根据总数/每页显示数得到总页数
    let totalPage = Math.ceil(total / size);
    //limit是每页显示数，skip通过（当前页-1）*每页显示得到
    let rows = await servicesModel
        .find(searchObj)
        .limit(parseInt(size))
        .skip((current - 1) * size)
        .populate({ path: "shopsId", model: shopsModel })
        .populate('adminId')
    return {
        current, size, total, totalPage, rows
    };
}
//增加
module.exports.insert = async function (cinemas) {
    return await servicesModel.create(cinemas);
}
//根据ID删除
module.exports.delete = async function (id) {
    return await servicesModel.deleteOne({ _id: id });
}
//根据ID查询
module.exports.findById = async function (id) {
    return await servicesModel.findById(id);
}
//修改
module.exports.update = async function (id, cinemas) {
    return await servicesModel.updateOne({ _id: id }, cinemas);
}